package com.fly.estore.dao.Impl;

import com.fly.estore.dao.BaseDAO;
import com.fly.estore.dao.ProductCatalogDAO;
import com.fly.estore.domain.ProductCatalog;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/16.
 */
@Repository("ProductCatalogDAO")
public class ProductCatalogDAOImpl extends BaseDAO<ProductCatalog> implements ProductCatalogDAO {

    public int addProductCatalog(final ProductCatalog catalog) {
        int update = 0;
        StringBuilder sql = new StringBuilder("insert into product_catalog(product_catalog_id, catalog_name, order_num, discount, create_id, create_time)");
        sql.append("values (?, ?, ?, ?, ?, ?)");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, catalog.getProductCatalogId());
                ps.setString(i++, catalog.getCatalogName());
                ps.setInt(i++, catalog.getOrderNum());
                ps.setString(i++, catalog.getDiscount());
                ps.setString(i++, catalog.getCreateId());
                ps.setString(i++, catalog.getCreateTime());
            }
        });
        return update;
    }

    public int updateProductCatalog(final ProductCatalog catalog) {
        int update = 0;
        StringBuilder sql = new StringBuilder("update product_catalog set ");
        sql.append(" catalog_name = ?,")
                .append(" order_num = ?,")
                .append(" discount = ?,")
                .append(" last_update_id = ?,")
                .append(" last_update_time = ?")
                .append(" where product_catalog_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, catalog.getCatalogName());
                ps.setInt(i++, catalog.getOrderNum());
                ps.setString(i++, catalog.getDiscount());
                ps.setString(i++, catalog.getLastUpdateId());
                ps.setString(i++, catalog.getLastUpdateTime());
                ps.setString(i++, catalog.getProductCatalogId());
            }
        });
        return update;
    }

    public int deleteById(final String productCatalogId) {
        int update = 0;
        String sql = "delete from product_catalog where product_catalog_id = ?";
        update = getJdbcTemplate().update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, productCatalogId);
            }
        });
        return update;
    }

    public ProductCatalog getById(String productCatalogId) {
        ProductCatalog catalog = new ProductCatalog();
        String sql = "select * from product_catalog where product_catalog_id = ? order by order_num";
        List<ProductCatalog> resultList = getJdbcTemplate().query(sql, new CatalogRowMapper(), new Object[]{productCatalogId});
        if(!CollectionUtils.isEmpty(resultList)){
            catalog = resultList.get(0);
        }
        return catalog;
    }

    public List<ProductCatalog> getList() {
        List<ProductCatalog> catalogList = new ArrayList<ProductCatalog>();
        String sql = "select * from product_catalog order by order_num";
        catalogList = getJdbcTemplate().query(sql, new CatalogRowMapper());
        return catalogList;
    }

    public class CatalogRowMapper implements RowMapper<ProductCatalog> {
        public ProductCatalog mapRow(ResultSet rs, int rowNum) throws SQLException {
            ProductCatalog catalog = new ProductCatalog();
            catalog.setProductCatalogId(rs.getString("product_catalog_id"));
            catalog.setCatalogName(rs.getString("catalog_name"));
            catalog.setOrderNum(rs.getInt("order_num"));
            catalog.setDiscount(rs.getString("discount"));
            catalog.setCreateId(rs.getString("create_id"));
            catalog.setCreateTime(rs.getString("create_time"));
            catalog.setLastUpdateId(rs.getString("last_update_id"));
            catalog.setLastUpdateTime(rs.getString("last_update_time"));
            return catalog;
        }
    }
}
